package com.recipeexchange.utils

import com.recipeexchange.domain.Recipe


class GenerateLocator {
    static String generate(String title){
        String locator = removeIllegalCharacters(title)

        return generateNonDuplicate( locator )
    }

    private static String removeIllegalCharacters(String title) {
        return title.replaceAll(" ", "-").replaceAll("'", "").replaceAll('"', "")
    }

    private static String generateNonDuplicate(String locator){
        int numberOfLocators = 2
        String updatedLocator = locator
        while(Recipe.countByLocator(updatedLocator) > 0){
            updatedLocator = "$locator-$numberOfLocators"
            numberOfLocators++
        }
        return updatedLocator.toLowerCase()
    }
}
